﻿using GalaSoft.MvvmLight.Command;
using HuiZe.education.Model;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace HuiZe.education.View
{
    /// <summary>
    /// PART_MyLesson_Item.xaml 的交互逻辑
    /// </summary>
    public partial class PART_MyLesson_Item : UserControl
    {
        public resCourseListChildModel Data
        {
            get { return (resCourseListChildModel)GetValue(DataProperty); }
            set { SetValue(DataProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Data2.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DataProperty =
            DependencyProperty.Register("Data", typeof(resCourseListChildModel), typeof(PART_MyLesson_Item), new PropertyMetadata(null, (sender, e) =>
            {
                PART_MyLesson_Item _item = (PART_MyLesson_Item)sender;
                resCourseListChildModel _data = null;
                if (e.NewValue != null) _data = (resCourseListChildModel)e.NewValue;

                _item.Load(_data);
            }));



        public Action<resCourseListChildModel> SelectCallback { get; set; }
        public Action<resCourseListChildModel> DeleteCallback { get; set; }

        public PART_MyLesson_Item()
        {
            InitializeComponent();
            PART_Delete.MouseLeftButtonUp += PART_Delete_MouseLeftButtonUp;
            PART_IconBorder.MouseLeftButtonUp += PART_MyLesson_Item_MouseLeftButtonUp;
        }

        void PART_Delete_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (DeleteCallback != null)
                DeleteCallback(Data);
        }

        void PART_MyLesson_Item_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (SelectCallback != null)
                SelectCallback(Data);
        }

        public void Load(resCourseListChildModel _data)
        {
            if (_data == null)
            {
                PART_FileName.Text = string.Empty;
                PART_FileIcon.ImageSource = null;
            }
            else
            {
                PART_FileName.Text = _data.resources_name;

                string _url = _data.file;
                switch (_data.ext_name)
                {
                    case "pdf": _url = "pack://application:,,,/HuiZe.education;component/Ethan/Res/Images/MySpace/pdf.png"; break;
                    case "mp4": _url = "pack://application:,,,/HuiZe.education;component/Ethan/Res/Images/MySpace/mp4.png"; break;
                    case "doc": _url = "pack://application:,,,/HuiZe.education;component/Ethan/Res/Images/MySpace/doc.png"; break;
                    case "docx": _url = "pack://application:,,,/HuiZe.education;component/Ethan/Res/Images/MySpace/doc.png"; break;
                    case "mp3": _url = "pack://application:,,,/HuiZe.education;component/Ethan/Res/Images/MySpace/mp3.png"; break;
                    case "ppt": _url = "pack://application:,,,/HuiZe.education;component/Ethan/Res/Images/MySpace/ppt.png"; break;
                    case "pptx": _url = "pack://application:,,,/HuiZe.education;component/Ethan/Res/Images/MySpace/ppt.png"; break;
                    case "swf": _url = "pack://application:,,,/HuiZe.education;component/Ethan/Res/Images/MySpace/swf.png"; break;
                    case "txt": _url = "pack://application:,,,/HuiZe.education;component/Ethan/Res/Images/MySpace/txt.png"; break;
                    case "xls": _url = "pack://application:,,,/HuiZe.education;component/Ethan/Res/Images/MySpace/xls.png"; break;
                    case "xlsx": _url = "pack://application:,,,/HuiZe.education;component/Ethan/Res/Images/MySpace/xls.png"; break;
                    case "zip": _url = "pack://application:,,,/HuiZe.education;component/Ethan/Res/Images/MySpace/zip.png"; break;
                }
                if (!string.IsNullOrEmpty(_data.cover))
                    _url = _data.cover;

                if (!string.IsNullOrEmpty(_url))
                {
                    BitmapImage _bmp = new BitmapImage();
                    _bmp.CacheOption = BitmapCacheOption.OnLoad;
                    _bmp.BeginInit();
                    _bmp.UriSource = new Uri(_url, UriKind.RelativeOrAbsolute);
                    _bmp.EndInit();

                    if (_url.StartsWith("pack://"))
                        PART_FileIcon.Stretch = Stretch.Uniform;
                    else
                        PART_FileIcon.Stretch = Stretch.UniformToFill;

                    PART_FileIcon.ImageSource = _bmp;
                }
            }
        }
    }
}
